package org.apache.doris.demo.spring.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.doris.demo.spring.model.EvenData;
import org.apache.doris.demo.spring.model.Tag;
import org.apache.doris.demo.spring.model.UserTag;
import org.apache.doris.demo.spring.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Controller
@RequestMapping("/userProfile")
public class UserProfileController {
    @Autowired
    private UserTagService userTagService;

    @GetMapping("/{userId}")
    public String index(@PathVariable String userId, Model model) {
        List<UserTag> userTags = userTagService.list(new QueryWrapper<UserTag>().eq("userid", userId));
        String[] timelineData = {"2021", "2022", "2023"};
        List<EvenData> eventData = new ArrayList<>();
        eventData.add(new EvenData("2021年售假被处罚", "2021"));
        eventData.add(new EvenData("2022年被刑拘", "2022"));
        eventData.add(new EvenData("2023年被处罚", "2023"));
        ArrayList<Map<String, Object>> tagCloudList = new ArrayList<Map<String, Object>>();
        Random random = new Random();
        for (UserTag userTag : userTags) {
            Map<String, Object> data = new HashMap<>();
            int value = random.nextInt(10000 - 1000 + 1) + 1000;
            data.put("name", userTag.getTagName());
            data.put("value", value);
            tagCloudList.add(data);
        }
        List<PersonInfo> personInfoList = getPersonInfos();
        String jsonString = JSON.toJSONString(tagCloudList);
        model.addAttribute("timelineData", timelineData);
        model.addAttribute("eventData", eventData);
        model.addAttribute("tagCloudList", jsonString);
        model.addAttribute("personInfoList", personInfoList);
        return "userProfile/detail";
    }

    @GetMapping("/test/{userId}")
    public String indexTest(@PathVariable String userId, Model model) {
        List<UserTag> userTags = userTagService.list(new QueryWrapper<UserTag>().eq("userid", userId));
        String[] timelineData = {"2021", "2022", "2023"};
        List<EvenData> eventData = new ArrayList<>();
        eventData.add(new EvenData("2021年售假被处罚", "2021"));
        eventData.add(new EvenData("2022年被刑拘", "2022"));
        eventData.add(new EvenData("2023年被处罚", "2023"));
        List<Map<String, Object>> dataList = new ArrayList<>();
        Random random = new Random();
        for (UserTag userTag : userTags) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", userTag.getTagName());
            int value = random.nextInt(10000 - 1000 + 1) + 1000;
            data.put("value", value);
            dataList.add(data);
        }
        List<PersonInfo> personInfoList = getPersonInfos();
        JSONArray jsonArray = new JSONArray(Collections.singletonList(dataList));
        model.addAttribute("personInfoList", personInfoList);
        model.addAttribute("timelineData", timelineData);
        model.addAttribute("eventData", eventData);
        model.addAttribute("tagCloudList", jsonArray);
        model.addAttribute("personInfoList", personInfoList);
        return "/userProfile/list";
    }

    private static List<PersonInfo> getPersonInfos() {
        List<PersonInfo> personInfoList = new ArrayList<>();
        PersonInfo data = new PersonInfo("car", "车辆3台");
        personInfoList.add(data);
        data = new PersonInfo("case", "案件2个");
        personInfoList.add(data);
        data = new PersonInfo("associate", "关联人15个");
        personInfoList.add(data);
        data = new PersonInfo("address", "关联地点13个");
        personInfoList.add(data);
        return personInfoList;
    }

    @GetMapping("/add")
    public String addForm(Model model) {
        model.addAttribute("tag", new Tag());
        return "userProfile/add";
    }

    @PostMapping("/add")
    public String addTag(@ModelAttribute Tag tag) {
        tag.setTagId(LocalDate.now().toEpochDay() * 24 * 60 * 60);
        tag.setTimestamp(LocalDateTime.now());
        tag.setDate(LocalDate.now());
        return "redirect:/profiles";
    }

    @GetMapping("/edit/{name}")
    public String editForm(@PathVariable String name, Model model) {
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("tag_name", name);
        return "profiles/edit";
    }

    @PostMapping("/edit/{name}")
    public String editTag(@PathVariable String name, @ModelAttribute Tag tag) {
        QueryWrapper<Tag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("distinct_id", tag.getTagType());
        return "redirect:/profiles";
    }

    @GetMapping("/delete/{name}")
    public String deleteTag(@PathVariable String name) {
        Map<String, Object> tags = new HashMap<>();
        tags.put("tag_name", name);
        userTagService.removeByMap(tags);
        return "redirect:/profiles";
    }

    //    @ResponseBody
//    @GetMapping("/search/{keyword}")
//    public List<Tag> searchTags(@PathVariable ("keyword") String keyword) {
//        return userTagService.searchTagsByKeyword(keyword);
//    }
//    @ResponseBody
//    @GetMapping("/search/")
//    public List<Tag> searchAllTags() {
//        return userTagService.list();
//    }
    @lombok.Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Data {
        private String name;
        private int value;
    }
    @lombok.Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PersonInfo {
        private String name;
        private String value;
    }
}